package mxmf.content.code;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.springframework.stereotype.Component;

import mxmf.entity.CmsEntity;

@Component
public class ContentCodeGenerater implements CodeGenerater {

    DecimalFormat numberFormat = new DecimalFormat("000000");

    // 英文（program）+日期到秒（20180430184530）+6位序号（000001）
    @Override
    public String createCode(CmsEntity cmsEntity) {
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        StringBuilder code = new StringBuilder();
        code.append(cmsEntity.getClass().getSimpleName()).append("_");
        code.append(df.format(new Date())).append("_");
        if (cmsEntity.getId() == null) {
            code.append(UUID.randomUUID().toString());
        } else {
            code.append(numberFormat.format(cmsEntity.getId()));
        }
        return code.toString();
    }

}
